<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * 麦口支付宝支付
 * Class Maikou1Controller
 * @package Pay\Controller
 */
class Maikou2Controller extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;


    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));

    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }


        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                $pay_bankcode = 'ali_pay_wap_t0';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }



        // 构造要请求的参数数组
        $parameter = array(
            "merchantNo" => $this->config['mch_id'],
            "orderPrice" => intval($return['amount']),
            "outOrderNo" => $return['orderid'],
            "tradeType" => $pay_bankcode,
            "tradeTime" => date('YmdHis'),
            "goodsName" =>strval(mt_rand(1,10000)),

            "tradeIp" => getIP(),
            "returnUrl" => $return['callbackurl'],
            "notifyUrl" => $return['notifyurl'],
            //"remark" => strval(mt_rand(1,10000)),
        );

        ksort($parameter);
        $md5str = "";
        foreach ($parameter as $key => $val) {
            $md5str = $md5str . $key . "=" . $val . "&";
        }

        $parameter['sign']= strtoupper(md5($md5str . "secretKey=" . $this->config['signkey']));


        $res = curl($this->config['gateway'], httpstr($parameter));//curl_post
        $resp = json_decode($res,true);

        /*
array(4) {
  ["resultCode"]=>
  string(4) "0000"
  ["errMsg"]=>
  string(0) ""
  ["sign"]=>
  string(32) "26F8A20B83D600E3A6109B7E2AC96586"
  ["payMsg"]=>
  string(136) "http://www.zfpfgrant.com/wap/transfer2/1bf446eac5d083d14b2663f6743988b1/58a7b0dd5c298ddf71438ec7830454f883df7a3e9dfc50c1601f8c8319526ccf"
}
         *
         * */

        $code = $resp['payMsg'];
        if(!$code){
            if($resp['errMsg']){
                $this->showmessage($res['errMsg'], ['code' => C("THIRDCODES." . $this->class)]);
            }
            $this->showmessage('未获取到支付链接!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        header("location: {$code}");
    }



    //通知
    public function notifyurl(){
        $this->debug_callback();

        if(!$this->checkip(getIP(),['47.112.96.93'])){
            //exit("wrong");
        }
        $p = I('get.');
        /*
array (
  'orderPrice' => '50.00',
  'orderTime' => '20190510111612',
  'outOrderNo' => '2019051011161266573',
  'merchantNo' => 'CYFY2019050911380020',
  'goodsName' => '3520',
  'tradeType' => 'ali_pay_wap_t0',
  'successTime' => '20190510113905',
  'tradeStatus' => 'SUCCESS',
  'tradeNo' => 'P77772019051024043017',
  'sign' => 'BE1C4B597071F3A0F830D78317794A45',
)
         * */


            //获取订单信息
        $orderid = $p['outOrderNo'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['tradeNo'])));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        unset($p['sign']);

        ksort($p);
        $md5str = "";
        foreach ($p as $key => $val) {
            $md5str = $md5str . $key . "=" . $val . "&";
        }
        $sign1 = strtoupper(md5($md5str . "secretKey=" . $this->config['signkey']));

        if($sign1 !== $sign ){
            exit("sign_wrong");
        }

        if($p['tradeStatus'] !== 'SUCCESS'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'SUCCESS');
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }


    public function iosOrAndroid(){
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')||strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')){
            $type = true;
        }else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Android')){
            $type = false;
        }else{
            $type = false;
        }

        return $type;
    }


}